import six
from rest_framework.response import Response
from rest_framework.serializers import Serializer


class CCAIResponse(Response):
    def __init__(self, data=None, status=200, msg="成功",
                 template_name=None, headers=None,
                 exception=False, content_type=None, pagination=None, count=None):

        super(Response, self).__init__(None, status=status)

        if isinstance(data, Serializer):
            msg = (
                "You passed a Serializer instance as data, but "
                "probably meant to pass serialized `.data` or "
                "`.error`. representation."
            )
            raise AssertionError(msg)
        if status >= 400:
            msg = "失败"
        if pagination or count:
            self.data = {
                "code": status,
                "message": msg,
                "data": data,
                "count": count,
                "pagination": pagination
            }
        else:
            self.data = {
                "code": status,
                "message": msg,
                "data": data,
            }
        self.template_name = template_name
        self.exception = exception
        self.content_type = content_type

        if headers:
            for name, value in six.iteritems(headers):
                self[name] = value

